﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;

namespace Games.Common
{
    public static class Utils
    {
        private static Dictionary<string, int> dic = new Dictionary<string, int> {
            { "完美",0},
            { "杰出",2},
            { "优秀",10},
        };
        public static string GetLevel(int minCount, int originCount)
        {
            var ls = minCount / (dic.Values.Sum() * 2);
            var diff = originCount - minCount;
            foreach(var item in dic)
            {
                if(diff <= ls * item.Value)
                {
                    return item.Key;
                }
            }
            return "普通";
        }

        public static bool NoMaxLimit { get; set; }

        public static string FormatTime(int seconds)
        {
            var h = seconds / 3600;
            var m = seconds % 3600 / 60;
            var s = seconds % 60;
            if(h > 0)
            {
                return $"{h}:{m:D2}:{s:D2}";
            }
            else
            {
                return $"{m}:{s:D2}";
            }
        }

        private static Random rd = new Random(DateTime.Now.Millisecond);
        public static Color GetRandomColor(int level = 512)
        {
            var arr = new int[3];
            while(true)
            {
                var sum = 0;
                for(int i = 0; i < 3; i++)
                {
                    arr[i] = rd.Next(256);
                    sum += arr[i];
                }
                if(sum < level)
                {
                    return Color.FromArgb(arr[0], arr[1], arr[2]);
                }
            }
        }

        public static void Log(string text)
        {
            var logger = Xamarin.Forms.DependencyService.Get<ILogger>();
            logger.Log(text);
        }

        public static void Log(string meeage, Exception ex)
        {
            var logger = Xamarin.Forms.DependencyService.Get<ILogger>();
            logger.Log(meeage, ex);
        }
    }
}
